package sctu.edu.java.year2016.team06.chenqiang.day20160610.chapter15_2;

import javafx.scene.layout.Border;
import sctu.edu.java.year2016.team06.luoxin.day20160607.chapter15_2.PrintFrame;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by XQ on 2016/6/10.
 * 尝试开发一个窗体，在窗体中，有连个按钮，一个是开始按钮，一个是结束按钮
 * 当用户单击开始按钮时，持续打印一句话，当用户单击停止按钮时控制台结束打印。
 */
public class StartAndPause extends JFrame {
    private JPanel contentPane;
    Timer timer;
    int x = 1;
    private JTextArea jta;

    public static void main(String[] args) {
        StartAndPause frame = new StartAndPause();
        frame.setVisible(true);
    }

    public StartAndPause() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(350, 200);
        setLocationRelativeTo(null);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.SOUTH);
        JButton jbStart = new JButton("开始打印");
        panel.add(jbStart);
        timer = new Timer(500, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                jta.append("打印" + "第" + x + "行\r\n");
                x++;
            }
        });
        jbStart.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.start();
            }
        });
        JButton jbEnd = new JButton("停止打印");
        panel.add(jbEnd);
        jbEnd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                timer.stop();
            }
        });
        JScrollPane scrollPane = new JScrollPane();
        contentPane.add(scrollPane, BorderLayout.CENTER);
        jta = new JTextArea();
        scrollPane.setViewportView(jta);
    }

}
